﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCPhase2Tester.Daemons;

namespace IRCPhase2Tester.Tests
{
    class NodeDownBroadcastSilence : MultiClientsMultiDaemons
    {
        #region ITest Members

        public override string Title()
        {
            return "Node Down Broadcast Silence";
        }

        public override void BuildUp()
        {
            StartNewDaemon(1, Session.Instance.AllConfigurationFiles[1], typeof(ExternalRoutingDaemon));
            StartNewDaemon(2, Session.Instance.AllConfigurationFiles[2], typeof(LocalRoutingDaemon));
            StartNewClient(this.daemons[0].IRCLocalPort);

            base.BuildUp();
        }

        public override bool RunTest()
        {
            clients[0].Send("adduser test1");
            if (clients[0].Receive().ToLower().Trim() != "ok")
            {
                return false;
            }

            System.Threading.Thread.Sleep(5000);

            int lastSequenceNumber = -1;

            this.daemons[1].Silence();

            System.Threading.Thread.Sleep(360000);

            lastSequenceNumber = RoutingDaemon.Backend.DaemonBackEnd.Instance.GetNodeByID(1).LastSequenceNumber;

            int attempts = 5;
            while (attempts-- > 0)
            {
                if (RoutingDaemon.Backend.DaemonBackEnd.Instance.GetNodeByID(1).LastSequenceNumber != lastSequenceNumber)
                {
                    return false;
                }
                System.Threading.Thread.Sleep(10000);
            }
            return true;
        }

        public override int NumberOfMinutes()
        {
            return 10;
        }

        #endregion
    }

}